#!/bin/bash

echo "检查所有包含ListByPagination方法的文件..."
echo "=========================================="

# 定义service文件目录
SERVICE_DIR="fuint-application/src/main/java/com/fuint/common/service/impl"

# 查找所有包含ListByPagination方法的文件
echo "查找包含ListByPagination方法的文件："
echo ""

all_pagination_files=()
problem_files=()

# 遍历所有ServiceImpl.java文件
for file in $(find $SERVICE_DIR -name "*ServiceImpl.java" -not -name "*.bak"); do
    # 检查是否包含ListByPagination方法
    if grep -q "ListByPagination" "$file"; then
        filename=$(basename "$file")
        all_pagination_files+=("$filename")
        echo "📄 $filename - 包含ListByPagination方法"
        
        # 检查是否在分页方法中使用了selectList
        if grep -A 20 -B 5 "ListByPagination" "$file" | grep -q "selectList"; then
            echo "   ❌ 发现问题：使用了selectList"
            problem_files+=("$filename")
            
            # 显示具体的问题行
            echo "   问题方法："
            grep -n -A 10 -B 2 "ListByPagination" "$file" | grep -A 10 -B 2 "selectList" | head -15
        else
            echo "   ✅ 已修复或无问题"
        fi
        echo ""
    fi
done

echo "=========================================="
echo "检查结果汇总："
echo "=========================================="

echo "包含ListByPagination方法的文件总数: ${#all_pagination_files[@]}"
echo ""

if [ ${#problem_files[@]} -eq 0 ]; then
    echo "✅ 没有发现问题文件"
else
    echo "❌ 发现 ${#problem_files[@]} 个问题文件："
    for file in "${problem_files[@]}"; do
        echo "  - $file"
    done
fi

echo ""
echo "所有包含ListByPagination方法的文件："
for file in "${all_pagination_files[@]}"; do
    echo "  - $file"
done

echo ""
echo "修复建议："
echo "=========================================="
echo "将分页方法中的："
echo "  List<Entity> dataList = mapper.selectList(lambdaQueryWrapper);"
echo "替换为："
echo "  Page<Entity> page = new Page<>(currentPage, pageSize);"
echo "  Page<Entity> pageResult = mapper.selectPage(page, lambdaQueryWrapper);"
echo "  List<Entity> dataList = pageResult.getRecords();"
